
function getImageFullURLPath(avatar_url) {
    return "http://localhost:8080/" + avatar_url;
}
layui.use(function(){
    var upload = layui.upload;
    var layer = layui.layer;
    console.log(123)
    // 渲染
    upload.render({
        elem: '#avatar',
        url: 'http://localhost:8080/image/uploadAvatar',
        done: function(res){
            layer.msg('上传成功');
            console.log(res);
            let $avatar = $("#avatar-preview");
            $avatar.attr("src",res.data.url);
            $avatar.toggle(true);

            if(res.code === 0){
                var value = res.data.url.substring(res.data.url.lastIndexOf("/img"));
                console.log(value);
                $("#avatarUrl").val(value);
            }
        }
    });
});


function checkLogin(){
    console.log("checking login")
    $.post({
        url: 'http://localhost:8080/login/checkLogin',
        async:false,
        method: 'POST',
        crossDomain: true,
        xhrFields: { withCredentials: true },
        success: function (msg) {
            let user = msg.data.user;
            //    user是
            if(user == null){
                layer.msg("请登录");
                window.location.href='../home.html';
            }
            window.user = user;
        }, error: function (error) {
            console.log(error)
        }
    })
}



function initInfo() {
    console.log(user.more.nickname)
    $("#avatarUrl").val(user.more.avatarUrl);
    $("#avatar-preview").attr("src",getImageFullURLPath(user.more.avatarUrl));
    $("#avatar-preview").toggle();
    $("#nickname").val(user.more.nickname);
    $("#signature").val(user.more.signature);
    let str = "<option";
    if(user.more.gender == '男'){
        str += "selected='selected'";
    }
    str += ">男</option>";
    $("#gender").append(str);
    str ="<option";
    if(user.more.gender == '女'){
        str += "selected='selected'";
    }
    str += ">男</option>";
    $("#gender").append(str);
}

function loadData(){
    checkLogin();
    $.post({
        url: 'http://localhost:8080/province/list',
        async:false,
        method: 'POST',
        crossDomain: true,
        xhrFields: { withCredentials: true },
        success: function (msg) {
            console.log(msg);
            initInfo();
            loadProvince(msg.data.list,true);
            loadCity(true);
        }, error: function (error) {
            layer.msg("省份获取失败~");
            console.log(error)
        }
    })
}

function loadProvince(province,first){
    let $province = $("#province");
    $(province).each(function (i,e){
        let option = "<option value='" + e.id +"'";
        if(user.more.provinceId === e.id && first === true){
            option += "selected='selected'";
        }
        option += ">"+e.name+"</option>";
        $province.append(option);
    })
}

function loadCity(first){
    let val = $("#province").val();
    console.log("当前选择的省id是")
    console.log(val);
    let $city = $("#city");
    $.post({
        url: 'http://localhost:8080/city/listByProvinceId',
        async:false,
        method: 'POST',
        data:{
            "id":val
        },
        crossDomain: true,
        xhrFields: { withCredentials: true },
        success: function (msg) {
            console.log(msg);
            let list = msg.data.list;
            $(list).each(function (i,e){
                $city.empty();
                let option = "<option value='" + e.id +"'";
                if(first === true){
                    option += "selected='selected'"
                }
                option += ">"+e.name+"</option>";
                $city.append(option);
            })
        }, error: function (error) {
            layer.msg("城市获取失败~");
            console.log(error)
        }
    })

}

function uploadAll(){
    let data = $("#main-form").serialize();
    console.log(data);
    $.post({
        url: 'http://localhost:8080/userPersonalInfo/update',
        async:false,
        method: 'POST',
        data:data,
        crossDomain: true,
        xhrFields: { withCredentials: true },
        success: function (msg) {
            console.log(msg);
            window.location.href='info.html?id='+user.base.id;
        }, error: function (error) {
            layer.msg("保存失败~");
            console.log(error)
        }
    })
}